<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import loginInfo from '@/api/login.js';
	export default {
		onLaunch: async function() {
			console.log('App Launch')
			// 如果用户已经初始化 不再进入启动页
			const token = uni.getStorageSync('token')
			if (token) {
				this.updateHomeLoading({
					homeLoading: false
				});
				uni.switchTab({
					url: '/pages/index/index'
				})
			} else {
				// 1. 获取微信jsCode
				const loginRes = await uni.login({
					provider: 'weixin',
					onlyAuthorize: true
				});
				const jsCode = loginRes.code;
				// 2. 发送jsCode到后端
				const res = await loginInfo.loginApi(jsCode);
				if (res.data.code == 200 && res.data.data?.token) {
					// 登录成功，保存token和用户信息
					uni.setStorageSync('token', res.data.data?.token);
					uni.setStorageSync('userId', res.data.data?.userId);
					uni.setStorageSync('bindCode', res.data.data?.bindCode);
					uni.setStorageSync('bindType', res.data.data?.bindType);
					uni.switchTab({
						url: '/pages/index/index'
					})
				} else {
					uni.reLaunch({
						url: '/pages/entryPage/entryPage'
					})
				}
				this.updateHomeLoading({
					homeLoading: false
				});
			}
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
			// 如果用户已经初始化 本地记录
			// uni.setStorageSync('flag', 1)
			this.ceshi(true)
		},
		onExit() {
			console.log('App onExit')
			// uni.setStorageSync('flag', 1)
			this.ceshi(true)
		},
		computed: {
			...mapState(['ceshidata', 'homeLoading'])
		},
		methods: {
			...mapMutations(['ceshi', 'updateHomeLoading'])
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/uview-ui/index.scss";
	@import "@/common/css/common.css";
	@import './tuniao-ui/index.scss';
	@import './tuniao-ui/iconfont.css';
	/* @import "@/static/font/iconfont.css"; */
</style>